import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _startsWith from "lodash/startsWith";
import _set from "lodash/set";
import _map from "lodash/map";
import _forEach from "lodash/forEach";
import _cloneDeep from "lodash/cloneDeep";
import { modalBtnPrefix, subEventPrefix } from 'components/DynamicComponent/config';
import { hiddenColumnPrefix } from 'components/DynamicComponent/DynamicTable/utils';
import DataType from "../DataType";
import { noop, commonParseForField, getNewUUIDKey } from "./common";
/**
 * 将 存储的 dynamicTable 转换成 设计器 可以识别的
 * @param {object} template
 * @param {object} options
 * @returns {object}
 */

function dynamicTableDirtyExtra(template, options) {
  var noCloneDeep = options && options.noCloneDeep;
  var newTemplate = noCloneDeep ? template : _cloneDeep(template);
  var props = {};
  var hiddenColumns = [];
  var newConfig = [];

  _forEach(newTemplate.config, function (prop) {
    if (_startsWith(prop.attributeName, hiddenColumnPrefix)) {
      hiddenColumns.push(prop);
    } else {
      newConfig.push(prop);
      props[prop.attributeName] = prop.value;
    }
  });

  newTemplate.config = newConfig;
  newTemplate.hiddenColumns = hiddenColumns; // 批量处理 DynamicTable 的 字段 (虽然只处理了 LinkButton)

  newTemplate.fields = _map(newTemplate.fields, function (field) {
    var extraField = dynamicTable.dirtyExtraFields[field.componentType] || noop;
    extraField(newTemplate, field);
    return field;
  });
  newTemplate.pagination = props.pagination;

  if (props.pagination) {
    newTemplate.defaultPageSize = props.defaultPageSize;
  }

  return newTemplate;
}

function dynamicTableInitExtra(template) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var noCloneDeep = options && options.noCloneDeep;
  var newTemplate = noCloneDeep ? template : _cloneDeep(template);
  return newTemplate;
}

function dynamicTableDirtyParse(template) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

  var _ref = options || {},
      orderKeys = _ref.orderKeys,
      originKeyMapIndex = _ref.originKeyMapIndex,
      noCloneDeep = _ref.noCloneDeep;

  var newTemplate = noCloneDeep ? template : _cloneDeep(template);
  var orderSeq = 0;
  var orderSeqStep = 10;
  var newFields = [];

  _forEach(orderKeys, function (key) {
    var field = newTemplate.fields[originKeyMapIndex[key]];
    commonParseForField(newTemplate, field);
    var parseField = dynamicTable.dirtyParseFields[field.componentType] || noop;
    parseField(newTemplate, field);
    newFields.push(_objectSpread(_objectSpread({}, field), {}, {
      orderSeq: orderSeq += orderSeqStep,
      // todo table是否没有这两个字段
      enabledFlag: 1,
      requiredFlag: 0
    }));
  });

  newTemplate.fields = newFields;
  newTemplate.enabledFlag = 1; // warn dynamicTable enabledFlag 字段必输, 但是没有地方编辑

  newTemplate.config = newTemplate.config || [];

  _forEach(newTemplate.hiddenColumns, function (hiddenColumn) {
    newTemplate.config.push(hiddenColumn);
  });

  delete newTemplate.hiddenColumns;
  delete newTemplate.pagination; // warn dynamicTable's attr of pagination use in designer

  delete newTemplate.defaultPageSize; // warn dynamicTable's attr of defaultPageSize use in designer

  return newTemplate;
}
/**
 * 新加进来的 字段 宽度统一为 120
 * @param {object} template
 * @param {object} field
 * @param {object} options
 * @returns
 */
// eslint-disable-next-line no-unused-vars


function dynamicTableInitExtraField(template, field, options) {
  field.width = 120; // 每次 Table 的 fields 顺序 和 数量 发生变化 都必须是新的 设计器

  template.renderKey = getNewUUIDKey();
}
/**
 * @param {object} template
 * @param {object} field
 * @param {object} options
 * @returns
 */
// eslint-disable-next-line no-unused-vars


function dynamicTableDirtyExtraFieldLinkButton(template, field, options) {
  // todo 确定 DynamicTable 的 linkButton 没有其他的属性值, 且只有一级属性
  var props = {};
  var btns = [];

  _forEach(field.config, function (attr) {
    _set(props, attr.attributeName, attr);
  });

  Object.keys(props).forEach(function (btnKey) {
    var btnProps = props[btnKey];
    var btnConfig = [];
    var orderSeq;
    Object.keys(btnProps).forEach(function (btnPropKey) {
      if (btnPropKey === 'orderSeq') {
        orderSeq = btnProps[btnPropKey].value;
      } else if (btnPropKey === 'modalBtns') {
        btnConfig.push({
          attributeName: 'modalBtns',
          value: btnProps.modalBtns
        });
        return;
      } else if (btnPropKey === 'subEvents') {
        btnConfig.push({
          attributeName: 'subEvents',
          value: btnProps.subEvents
        });
        return;
      } // 将对应的 attr 放进按钮中


      btnProps[btnPropKey].attributeName = btnPropKey;
      btnConfig.push(btnProps[btnPropKey]);
    });
    btns[orderSeq] = btnConfig; // btns.push(btnConfig);
  });
  field.btns = btns;
  return field;
}

function dynamicTableDirtyParseFieldLinkButton(template, field) {
  // todo 确定 DynamicTable 的 linkButton 没有其他的属性值, 且只有一级属性
  var config = [];

  _forEach(field.btns, function (btnConfigs, index) {
    var btnKey;

    _forEach(btnConfigs, function (attr) {
      switch (attr.attributeName) {
        case 'btnKey':
          btnKey = attr.value; // 找到 btnKey 的属性

          break;

        default:
          break;
      }
    });

    config.push({
      attributeName: "[".concat(btnKey, "][orderSeq]"),
      attributeType: DataType.Number,
      value: index
    }); // 过滤掉 orderSeq 属性

    _forEach(btnConfigs, function (btnConfig) {
      if (btnConfig.attributeName === 'modalBtns') {
        // 设置 modalBtns 的属性
        btnConfig.attributeName = "[".concat(btnKey, "]").concat(btnConfig.attributeName);
        var modalBtns = btnConfig.value;

        for (var i = 0; i < modalBtns.length; i += 1) {
          config.push({
            attributeName: "[".concat(btnKey, "]").concat(modalBtnPrefix, "[").concat(i, "]"),
            value: modalBtns[i].value,
            attributeType: DataType.String
          });
        }
      } else if (btnConfig.attributeName === 'subEvents') {
        // 设置 subEvents 的属性
        btnConfig.attributeName = "[".concat(btnKey, "]").concat(btnConfig.attributeName);
        var subEvents = btnConfig.value;

        for (var _i = 0; _i < subEvents.length; _i += 1) {
          config.push({
            attributeName: "[".concat(btnKey, "]").concat(subEventPrefix, "[").concat(_i, "]"),
            attributeType: DataType.String,
            value: subEvents[_i].value
          });
        }
      } else if (btnConfig.attributeName !== 'orderSeq') {
        btnConfig.attributeName = "[".concat(btnKey, "][").concat(btnConfig.attributeName, "]");
        config.push(btnConfig);
      }
    });
  });

  delete field.btns;
  field.config = config;
}

var dynamicTable = {
  initExtra: dynamicTableInitExtra,
  initExtraField: dynamicTableInitExtraField,
  dirtyExtra: dynamicTableDirtyExtra,
  dirtyParse: dynamicTableDirtyParse,
  dirtyExtraFields: {
    LinkButton: dynamicTableDirtyExtraFieldLinkButton
  },
  dirtyParseFields: {
    LinkButton: dynamicTableDirtyParseFieldLinkButton
  }
};
export default dynamicTable;